package com.example.app1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class IntentSend9Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent_send_9);
    }


    public void onClick(View view) {
        RadioButton radioMan = (RadioButton) findViewById(R.id.radio_man);
        RadioButton radioWoman = (RadioButton) findViewById(R.id.radio_woman);

        EditText heightText = findViewById(R.id.height_input);
        EditText weightText = findViewById(R.id.weight_input);
        System.out.printf(weightText.getText().toString());
        Double weight = Double.valueOf(weightText.getText().toString());
        Double height = Double.valueOf(heightText.getText().toString())/100;
        Double bim = weight / Math.pow(height,2);
        Double lowWeight ,highWeight;
        Intent intent = new Intent(this,IntentReceive9Activity.class);
        if (radioMan.isChecked()) {
            lowWeight = 20.7*Math.pow(height,2);
            highWeight = 26.4*Math.pow(height,2);

            StringBuilder msg = new StringBuilder(String.format("您的合适的体重范围为：%.2f~%.2f\n身体质量指数为%.2f",lowWeight,highWeight,bim));
            if(bim < 17.5){
                msg.append("，属于厌食，\n请尽快调整！");
            }else if (bim < 20.7){
                msg.append("，属于偏轻，\n请适当调整！");
            }else if (bim <26.4){
                msg.append("，属于正常范围，\n请继续保持！");
            }else if (bim < 27.8){
                msg.append("，属于略微超重，\n请适当调整");
            }else{
                msg.append("，属于超重，\n请尽快调整");
            }
            intent.putExtra("msg",msg.toString());
            startActivity(intent);
        }else if (radioWoman.isChecked()){
            lowWeight = 19.1*Math.pow(height,2);
            highWeight = 25.8*Math.pow(height,2);

            StringBuilder msg = new StringBuilder(String.format("您的合适的体重范围为：%.2f~%.2fkg\n身体质量指数为%.2f",lowWeight,highWeight,bim));
            if(bim < 17.5){
                msg.append("，属于厌食，\n请尽快调整！");
            }else if (bim < 19.1){
                msg.append("，属于偏轻，\n请适当调整！");
            }else if (bim <25.8){
                msg.append("，属于正常范围，\n请继续保持！");
            }else if (bim < 27.3){
                msg.append("，属于略微超重，\n请适当调整");
            }else{
                msg.append("，属于超重，\n请尽快调整");
            }
            intent.putExtra("msg",msg.toString());
            startActivity(intent);
        }else {
            printError("请输入性别");
        }
    }

    private void printError(String s){
        Toast toast = Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM,0,200);
    }
}